package store.controller;

import com.alibaba.fastjson.JSONObject;
import store.dao.CategoryDAO;
import store.po.Category;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/category")
public class GetCategory extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        try {
            Category c=new CategoryDAO().findById(id);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("success",true);
            jsonObject.put("data",c);
            response.setContentType("application/x-json");
            response.getWriter().write(jsonObject.toJSONString());
            response.getWriter().flush();
            response.getWriter().close();
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("success",false);
            jsonObject.put("msg",e.getMessage());
            response.setContentType("application/x-json");
            response.getWriter().write(jsonObject.toJSONString());
            response.getWriter().flush();
            response.getWriter().close();
        }
    }
}
